//
//  CatCell2.m
//  代号：47
//
//  Created by 杨佩璋 on 15/7/3.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "CatCell2.h"
#import <UIImageView+WebCache.h>


@implementation CatCell2

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, kScreen_width - 20, 40)];
        _titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _titleLabel.textColor = [UIColor orangeColor];
        CGFloat picWidth = (kScreen_width - 30)/3;
        CGFloat picHeight = picWidth / 1.5 + 20;
        _pic1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 60, picWidth, picHeight)];
        _pic2 = [[UIImageView alloc] initWithFrame:CGRectMake(10 + picWidth + 5, 60, picWidth, picHeight)];
        _pic3 = [[UIImageView alloc] initWithFrame:CGRectMake(10 + picWidth + 5 + picWidth + 5, 60, picWidth, picHeight)];
        _pic1.contentMode = UIViewContentModeScaleAspectFill ;
        _pic2.contentMode = UIViewContentModeScaleAspectFill;
        _pic3.contentMode = UIViewContentModeScaleAspectFill;
        _pic1.clipsToBounds = YES;
        _pic2.clipsToBounds = YES;
        _pic3.clipsToBounds = YES;
        _commentCount = [[UILabel alloc] initWithFrame:CGRectMake(10, 60 + picHeight +10, picWidth, 20)];
        _commentCount.font = [UIFont systemFontOfSize:10];
        _commentCount.textColor = [UIColor lightGrayColor];
        CGFloat titleWidth = kScreen_width - 120 -10;

        _publishTime = [[UILabel alloc] init];
        _publishTime.frame = CGRectMake(120, 60 + picHeight +10, titleWidth, 20);
        _publishTime.font = [UIFont systemFontOfSize:10];
        _publishTime.textColor = [UIColor lightGrayColor];
        _publishTime.textAlignment = NSTextAlignmentRight;
        
        
        
        [self.contentView addSubview:_pic3];
        [self.contentView addSubview:_pic1];
        [self.contentView addSubview:_pic2];
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_commentCount];
        [self.contentView addSubview:_publishTime];
        
        
        
        [_pic3 release];
        [_pic1 release];
        [_pic2 release];
        [_commentCount release];
        [_titleLabel release];
        [_publishTime release];
        
        
    }
    return self;
}



-(void)setModel:(FestivalModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    NSString *url1 = model.images[0][@"url"];
    NSString *url2 = model.images[1][@"url"];
    NSString *url3 = model.images[2][@"url"];
    
    [_pic1 sd_setImageWithURL:[NSURL URLWithString:url1]];
    [_pic2 sd_setImageWithURL:[NSURL URLWithString:url2]];
    [_pic3 sd_setImageWithURL:[NSURL URLWithString:url3]];
    
    NSString *count = [NSString stringWithFormat:@"%@",model.commentCount];
    _commentCount.text = [NSString stringWithFormat:@"评论数%@",count];
    _titleLabel.text = model.title;
    
    
NSLog(@"%s %d %@",__FUNCTION__,__LINE__,model.publishTime);
    double time1 = [model.publishTime doubleValue];
    
    double time = time1 /1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSDate *currentDate = [NSDate date];
    NSDate *newDate = currentDate;
    NSTimeInterval minus = [newDate timeIntervalSinceDate:date];
    /*分钟*/
    NSTimeInterval minute =  minus/60;
    /*小时*/
    NSTimeInterval hour = minute/60;
    NSString *timeStr;
    if (minute < 60) {
        timeStr = [NSString stringWithFormat:@"%.0f分钟前",minute];
    }else if (hour<24){
        timeStr = [NSString stringWithFormat:@"%.0f小时前",hour];
    }else{
        timeStr = [NSString stringByDate:date];
    }
    _publishTime.text = timeStr;
    
}


-(void)dealloc{
    [_model release];
    [super dealloc];
}


@end
